<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("login.php");
}

$flyResultSet = getFlyById($_GET['fid']);

if ($_GET['page'] == '') {
    $start = 1;
    $page = 1;
} else {
    $start = ($_GET['page'] - 1) * 20 + 1;
    $page = $_GET['page'];
}

$commentsResultSet = getFlyComments($start, $start + 19, $_GET['fid']);

$comments = countFlyComments($_GET['fid']);

$pages = pagesFromMessages($comments);
$pagesStart = 0;
$pagesEnd = 0;

$page = $pages;

if ($pages <= 5) {
    $pagesStart = 1;
    $pagesEnd = $pages;
} else if ($pages >= 5 && $page + 2 <= $pages) {
    $pagesStart = $page - 2;
    $pagesEnd = $page + 2;
} else if ($pages >= 5 && $page + 2 > $pages) {
    $pagesStart = $pages - 4;
    $pages;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Мушки</title>
    <link rel="stylesheet" type="text/css" href="css/navigation.css">
    <link rel="stylesheet" type="text/css" href="css/fullnews.css">
    <link rel="stylesheet" type="text/css" href="css/comment.css">
    <link rel="stylesheet" type="text/css" href="css/square.css">
    <link rel="stylesheet" type="text/css" href="css/menu.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/fly.css">
    <script src="resources/jquery.js"></script>

    <meta charset="UTF-8">
    <script language="JavaScript">
        function reply(i) {
            $('#reply').val(i);
            $('#reply2').val(i);
            $('#textarea').focus();
            $('#textarea').attr("placeholder", "Вы процитировали сообщение, введите ваш ответ.. ")
        }

        function customFormSubmit() {
            $('#maximized_navigation').show();
            $('#commentform').submit();
        }
    </script>
</head>
<body class="body">

<?php
include_once("menu.php");
?>

<div id="square">
<div class="fullnews">
    <?php
    if ($fly = mysql_fetch_assoc($flyResultSet)) {
        $flyImagesResultSet = getFlyImages($fly['id'], $fly['mid']);
        ?>

        <div class="fliesheader">
            <?= htmlspecialchars($fly['header']) ?>
        </div>
        <div class="imagesdiv">
            <img src="fly_img/<?= $fly['path'] ?>" class="flyimg">

        </div>

        <div id="content" style="min-height: 25px;">
            <?= htmlspecialchars($fly['legend']) ?>
        </div>

        <div class="imagesdiv">
            <?php
            while ($flyImage = mysql_fetch_assoc($flyImagesResultSet)) {
                ?>
                <img src="fly_img/<?= $flyImage['path'] ?>" class="flyimg">
            <?php
            }
            ?>
        </div>

    <?php
    }
    ?>

    <div id="flyinfo">
        <div id="author">
            <?= $fly['author'] ?>
        </div>
        <div id="date">
            <?= $fly['date'] ?>
        </div>
        <div id="date">
            <?= $fly['time'] ?>
        </div>
    </div>
</div>

<div class="commentsheader">
    Комментарии пользователей:
</div>

<div id="pages">
    <?php

    if ($pages == 0) {
    }

    if ($page > 1) {
        ?>
        <div class="page"
             onclick="location.href='fly.php?fid=<?= $fly['id'] ?>&page=1'">
            <<
        </div>
    <?php
    }
    for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
        ?>
        <div class="page"
             onclick="location.href='fly.php?fid=<?= $fly['id'] ?>&page=<?= $i ?>'">
            <?= $i ?>
        </div>
    <?php
    }
    if ($page != $pages) {
        ?>
        <div class="page"
             onclick="location.href='fly.php?fid=<?= $fly['id'] ?>&page=<?= $pages ?>'">
            >>
        </div>
    <?php
    }
    ?>
</div>

<?php

while ($commentRow = mysql_fetch_assoc($commentsResultSet)) {
    ?>
    <div class="comment">
        <div class="commentinfodiv">
            <div class="commentauthor">
                #<?= $commentRow['id'] ?>
            </div>
            <div class="commentdate">
                <?= $commentRow['date'] ?>
            </div>
            <div class="commentdate">
                <?= $commentRow['time'] ?>
            </div>
        </div>

        <div>
            <div class="avatardiv">
                <div>
                    <img src="user_img/<?= $commentRow['img'] ?>" class="avatarimg">
                </div>
                <div class="userinfo">
                    <div>
                        Ник: <?= getTextWithAcronym($commentRow['author'], 15) ?>
                    </div>
                    <div>
                        Регистрация: <?= $commentRow['registered'] ?>
                    </div>
                    <div>
                        Сообщений: <?= $commentRow['msg'] ?>
                    </div>
                    <div>
                        Почта: <?= getTextWithAcronym($commentRow['email'], 22) ?>
                    </div>
                </div>
            </div>

            <div class="commentdiv">
                <?php if ($commentRow['quote'] != 0) { ?>
                    <div class="commentquote">
                        <?php
                        $quoteRow = mysql_fetch_assoc(getFlyComment($commentRow['quote']));
                        ?>
                        <?= htmlspecialchars($quoteRow['content']); ?>
                    </div>
                <?php
                }
                echo htmlspecialchars($commentRow['content']) ?>
            </div>
        </div>

        <div class="manipulationdiv" style="<?php if (!init()) echo "display: none;" ?>">
            <div class="manipulationbutton"
                <?php if (!isLiked("c", $commentRow['id'])) {
                    ?>
                    onclick="location.href = 'fly_comment_like_handler.php?fid=<?= $commentRow['id'] ?>&like=1'"
                <?php
                }
                ?>
                >
                +(<?= $commentRow['likes'] ?>)
            </div>
            <div class="manipulationbutton"
                <?php if (!isDisiked("c", $commentRow['id'])) {
                    ?>
                    onclick="location.href = 'fly_comment_like_handler.php?fid=<?= $commentRow['id'] ?>&dislike=1'"
                <?php
                }
                ?>
                >
                -(<?= $commentRow['dislikes'] ?>)
            </div>
            <?php if (!isSpammed("c", $commentRow['id'])) { ?>
                <div class="manipulationbutton"
                     onclick="location.href = 'fly_comment_spam_handler.php?fid=<?= $commentRow['id'] ?>'">
                    Спам
                </div>
            <?php
            }
            ?>
            <a href="#anchor">
                <div class="manipulationbutton"
                     style="margin-left: 160px;"
                     onclick="reply('<?= $commentRow['id'] ?>')">
                    Ответить
                </div>
            </a>
        </div>

    </div>

<?php
}
?>

<div id="pages">
    <?php

    if ($pages == 0) {
    }

    if ($page > 1) {
        ?>
        <div class="page"
             onclick="location.href='fly.php?fid=<?= $fly['id'] ?>&page=1'">
            <<
        </div>
    <?php
    }
    for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
        ?>
        <div class="page"
             onclick="location.href='fly.php?fid=<?= $fly['id'] ?>&page=<?= $i ?>'">
            <?= $i ?>
        </div>
    <?php
    }
    if ($page != $pages) {
        ?>
        <div class="page"
             onclick="location.href='fly.php?fid=<?= $fly['id'] ?>&page=<?= $pages ?>'">
            >>
        </div>
    <?php
    }
    ?>
</div>

<a id="anchor">
    <div class="common">
        <form enctype="multipart/form-data" accept-charset="UTF-8" action="fly_comment_handler.php" method="post"
              id="commentform">
            <span style="font-weight: bold">Новый комментарий:</span>
            <textarea name="content" id="textarea" rows="11" placeholder="Введите Ваш комментарий.."></textarea>

            <div style="display: inline-block; width: 100%; padding-left: 500px;">
                <div id="commentbutton" onclick="customFormSubmit()">Продолжить</div>
            </div>

            <input type="hidden" name="fid" value="<?= $_GET['fid'] ?>">
            <input type="hidden" name="reply" id="reply">
        </form>
    </div>
</a>
</div>


<?php
include_once("navigation.html");
?>

</body>
</html>